var aFunc = {
	plusReady: function() {
		if (mui.os.plus) {
			plus.navigator.closeSplashscreen();
			plus.screen.lockOrientation("portrait-primary");
			plus.navigator.setStatusBarBackground(common.colour.statusBackground);
			aVariable.webview.current = plus.webview.currentWebview();
			aVariable.webview.current.setStyle({
				bounce: "vertical",
				bounceBackground: common.colour.pageBackground,
			});
		}
		aVariable.webview.current.setStyle({
			bounce: "vertical",
			bounceBackground: common.colour.pageBackground,
		});

		aFunc.bindEvent();
		aFunc.initData();

	},


	initData: function() {
		var self = plus.webview.currentWebview();
		aVariable.calendar.year = self.year;
		aVariable.calendar.month = self.month;
		aVariable.calendar.day = self.day;
		aFunc.dateTopView(self.year, self.month, self.day, "今日", 'onlyYear');
		aVariable.view.calendarText.innerHTML = self.day;

		for (var i = 0; i < aVariable.calendar.month_name.length; i++) {
			var daysID = "days" + i;
			var monthID = "month" + i;
			var daysUl = document.getElementById(daysID);
			var monthUl = document.getElementById(monthID);
			var html = aFunc.creatCalendar(i, aVariable.calendar.year);
			daysUl.innerHTML = html;
			monthUl.innerHTML = aVariable.calendar.month_name[i];
		}
	},

	dateTopView: function(year, month, date, lunar, type) {
		if (type == "onlyYear") {
			aVariable.view.todayDate.innerHTML = year;
			aVariable.view.year.innerHTML = "";
			aVariable.view.lunar.innerHTML = "";
		} else {
			month += 1;
			aVariable.view.todayDate.innerHTML = month + "月" + date + "日";
			aVariable.view.year.innerHTML = year;
			aVariable.view.lunar.innerHTML = lunar;
		}
	},


	bindEvent: function() {
		aVariable.view.todayContent.addEventListener("tap", function() {
			aFunc.dateTopView(aVariable.calendar.year, aVariable.calendar.month, aVariable.calendar.day,
				"今日", 'onlyYear');
		});

		aVariable.view.calendarText.addEventListener("tap", function() {
			aFunc.dateTopView(aVariable.calendar.year, aVariable.calendar.month, aVariable.calendar.day,
				"今日");
		});

		//点击选择的日期
		mui('.body-list').on("tap", 'ul', function() {
			var card = this;
			var month = card.getAttribute("data-id");
			month = month.replace("days", "");
			month = parseInt(month) + 1;
			var opener = plus.webview.currentWebview().opener();
			mui.fire(opener, 'selectMonth', {
				year: aVariable.calendar.year,
				month: month,
				day: "1",
			});

			mui.back();

		});

	},


	//以下创建日历
	creatCalendar: function(mouth, year) {
		var htmsStr = "";
		var totalDay = aFunc.daysMonth(mouth, year); //获取该月总天数
		var firstDay = aFunc.dayStart(mouth, year); //获取该月第一天是星期几
		var myclass;

		var show_month = mouth + 1;

		if (firstDay <= 6) {
			for (var i = 1; i < firstDay; i++) {
				htmsStr += '<li  data-year="' + year + '" data-month="' + show_month +
					'" data-day="' + i + '"' + 'class= "daysli">' +
					'<div id =day' + i + ' class="calendar-item ' + myclass + '">' +
					'</div>' +
					'</li>';
			}
		}
		//本月
		for (var i = 1; i <= totalDay; i++) {
			if (i == aVariable.calendar.day && year == aVariable.calendar.year && mouth == aVariable.calendar
				.month) {
				myclass = "today-color"; //当天日期背景日期
			} else {
				myclass = "darkgrey";
			}
			htmsStr += '<li  data-year="' + year + '" data-month="' + show_month +
				'" data-day="' + i + '"' + 'class= "daysli">' +
				'<div id =day' + i + ' class="calendar-item ' + myclass + '">' +
				i +
				'</div>' +
				'</li>';
		}

		return htmsStr; //设置日期显示
	},

	//获取某年某月第一天是星期几
	dayStart: function(month, year) {
		var tmpDate = new Date(year, month, 1);
		return (tmpDate.getDay());
	},
	//获取某年某月最后一天是星期几
	dayEnd: function(month, year) {
		var monthNum = aFunc.daysMonth(month, year);
		var tmpDate = new Date(year, month, monthNum);
		return (tmpDate.getDay());
	},

	//获取该月总天数,计算某年是不是闰年，通过求年份除以4的余数即可 
	daysMonth: function(month, year) {
		year = parseInt(year);
		if ((year % 4 == 0 && year % 100 !== 0) || year % 400 == 0) {
			return (aVariable.calendar.month_olympic[month]);
		} else {
			return (aVariable.calendar.month_normal[month]);
		}
	}

};
